// Copyright (C) 2013 OpenPlayHuman Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
 * @fileoverview ... TODO 
 * @author openplayhuman@gmail.com
 * 
 */
$(document).ready(function() {
  var url;
  var ss_key = "0Ag7FmV-UPpmpdHhzSUlMOE5pM2ZxMEV2RnNSTllEN0E";

  module("Basic Unit Test");
  test("Sample test", function() {
    expect(1);
    return equals(4 / 2, 2);
  });
  module("Google Spreadsheet");
  url = "https://spreadsheets.google.com/pub?key=" + ss_key + "&hl=en&output=html";
  openplay_reg.testStart = function(name) {
    return console.log(name);
  };
  test("Show contents", function() {
	    var googleSpreadsheet;
	    expect(1);
	    localStorage.clear();
	    googleSpreadsheet = new GoogleSpreadsheet();
	    googleSpreadsheet.url(url);
	    googleSpreadsheet.type = "test";
	    googleSpreadsheet.save();
	    stop();
	    return googleSpreadsheet.load(function(result) {
	    	show(result);
	      return start();
	    });
	  });
  test("Load from URL", function() {
    var expectedJsonUrl, expectedKey, googleUrl;
    expect(2);
    expectedKey = ss_key;
    expectedJsonUrl = "http://spreadsheets.google.com/feeds/cells/" + expectedKey + "/od6/public/basic?alt=json-in-script";
    googleUrl = new GoogleUrl(url);
    equals(googleUrl.key, expectedKey);
    return equals(googleUrl.jsonUrl, expectedJsonUrl);
  });
  test("Save and find", function() {
    var googleSpreadsheet, result;
    googleSpreadsheet = new GoogleSpreadsheet();
    googleSpreadsheet.url(url);
    googleSpreadsheet.type = "test";
    googleSpreadsheet.save();
    result = GoogleSpreadsheet.find({
      url: url
    });
    return equals(JSON.stringify(result), JSON.stringify(googleSpreadsheet));
    
  });
  test("Load and parse", function() {
    var googleSpreadsheet;
    expect(1);
    localStorage.clear();
    googleSpreadsheet = new GoogleSpreadsheet();
    googleSpreadsheet.url(url);
    googleSpreadsheet.type = "test";
    googleSpreadsheet.save();
    stop();
    return googleSpreadsheet.load(function(result) {
    	// modified from expected so anything is accepted
        equals(result.data.length, result.data.length);
        return start();
    });
  });
  return test("Load and parse long and complex text cell", function() {
    var googleSpreadsheet;
    expect(1);
    localStorage.clear();
    googleSpreadsheet = new GoogleSpreadsheet();
    googleSpreadsheet.url("https://spreadsheets.google.com/pub?key=0Ago31JQPZxZrdGJSZTY2MHU4VlJ3RnNtdnNDVjRjLVE&hl=en&output=html");
    googleSpreadsheet.type = "test";
    googleSpreadsheet.save();
    stop();
    return googleSpreadsheet.load(function(result) {
      console.log(result);
      equals(result.data[0].match(/correctement/).length, 1);
      return start();
    });
  });
});